package com.infomanage.service.plan;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.EducationtrainPlanDAO;
import com.infomanage.dao.PersonDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.EducationtrainPlan;
import com.infomanage.po.Person;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.EducationtrainPlanItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

/**
 * 教育与培训计划
 *
 */
public class EducationTrainPlanService {
	public static EducationtrainPlanItem loadEducationTrainPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String trainId= request.getParameter("trainId");
			if(StringUtils.isEmpty(trainId)) return null;
			EducationtrainPlanDAO dao = new EducationtrainPlanDAO();
			return dao.find(Integer.parseInt(trainId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<EducationtrainPlanItem> loadEducationTrainPlanList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			EducationtrainPlanDAO dao = new EducationtrainPlanDAO();
			List<EducationtrainPlanItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveEducationTrainPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			EducationtrainPlan plan = new EducationtrainPlan();
			plan.setBeginTime(request.getParameter("beginTime"));
			plan.setEndTime(request.getParameter("endTime"));
			plan.setGrade(request.getParameter("grade"));//成绩
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			plan.setPlace(request.getParameter("place"));
			plan.setPosition(Integer.parseInt(request.getParameter("postion")));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				plan.setDeptId(person.getDeptId());
			}
			plan.setWay(request.getParameter("way"));
			plan.setDescription(request.getParameter("description"));
			EducationtrainPlanDAO dao = new EducationtrainPlanDAO();
			dao.add(plan);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("新增失败！");
		}
		return ret;
	}
	
	public static ReturnObject modifyEducationTrainPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int trainId = Integer.parseInt(request.getParameter("trainId"));
			EducationtrainPlanDAO dao = new EducationtrainPlanDAO();
			EducationtrainPlan plan = dao.find(EducationtrainPlan.class, trainId);
			plan.setBeginTime(request.getParameter("beginTime"));
			plan.setEndTime(request.getParameter("endTime"));
			plan.setGrade(request.getParameter("grade"));//成绩
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			plan.setPlace(request.getParameter("place"));
			plan.setPosition(Integer.parseInt(request.getParameter("postion")));
			plan.setWay(request.getParameter("way"));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				plan.setDeptId(person.getDeptId());
			}
			plan.setDescription(request.getParameter("description"));
			dao.modify(plan);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("修改失败！");
		}
		return ret;
	}
	
	public static ReturnObject removeEducationTrainPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String trainId = request.getParameter("trainId");
			EducationtrainPlanDAO dao = new EducationtrainPlanDAO();
			dao.remove(EducationtrainPlan.class, Integer.parseInt(trainId));
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
	
	public static List<EducationtrainPlanItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			EducationtrainPlanDAO dao = new EducationtrainPlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<EducationtrainPlanItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static ReturnObject removeBatchEducationTrainPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			EducationtrainPlanDAO dao = new EducationtrainPlanDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(EducationtrainPlan.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			EducationtrainPlanDAO dao = new EducationtrainPlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws Exception{
		request.setCharacterEncoding("UTF-8");
		String deptId = request.getParameter("deptId");
		String personId = request.getParameter("personId");
		SearchBean searchBean = new SearchBean();
		if(!StringUtil.isEmpty(deptId)){
			FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(personId)){
			FieldBean field = new FieldBean("personId", Integer.parseInt(personId));
			searchBean.add(field);
		}
		
		//设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if(user != null){
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
	return searchBean;
}
}
